﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    public class CourceNoticeController : BaseController
    {
        private readonly ITimeTableService _timeTableService;
        private readonly INoticeService _noticeService;

        public CourceNoticeController(ITimeTableService timeTableService,INoticeService noticeService)
        {
            _timeTableService = timeTableService;
            _noticeService = noticeService;
        }
        // GET: CourceNotice
        public ActionResult Create()
        {
            return View();
        }

        public ActionResult List4Teacher()
        {
            return View();
        }

        public ActionResult SaveCourceNotice(NoticeEntity vo)
        {
            try
            {
                if (vo.Title.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入标题！");
                }
                if (vo.Content.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入内容！");
                }
                vo.CreateTime = DateTime.Now;
                vo.NoticeType = 2;
                vo.CreateUserId = CurrentUser.UserId;
                _noticeService.Add(vo);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        [HttpGet]
        public ActionResult GetCourceList()
        {
            return Json(_timeTableService.GetCourceProjectListByTeacherId(CurrentUser.UserId).Select(x=>new { CourceId=x.CourceId,CourceName=x.CourceName}).Distinct(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetCourceNoticeList()
        {
            return Json(_noticeService.GetNoticeList(CurrentUser.UserId), JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult Delete(long noticeId)
        {
            _noticeService.Delete(noticeId);
            return Content("true");
        }
    }
}